import qs from 'qs'

let bossId = uni.getStorageSync('bossId') || ''
var token = uni.getStorageSync('token');
// const storeId = uni.getStorageSync('storeId')
const storeId = '1698904275582210048'
const post = function(url, data, success, fail, power, contentType, responseType = 'text') {
	var header = {};

	if (contentType == 0) {
		header['content-type'] = "application/json;charset=UTF-8";
	}
	if (contentType == 1) {
		header['content-type'] = "application/x-www-form-urlencoded;charset=UTF-8";
	}
		header['Authorization'] = token;
	if (data) {
		// data = qs.stringify(data);
		data.bossId = bossId
		data.storeId = storeId
	} else {
		data = null;
	}
	uni.request({
		url,
		method: 'POST',
		data,
		header,
		responseType,
		success(res) {
			if (res.statusCode == 200) {
			
				success && success(res.data)
			} else {
				fail && fail(res)
			}
			if(res.data.code == 403  || res.data.code == 401){   //拦截器，403 登陆过期
			uni.showToast({
				title:'登录失效，请重新登录',
				icon:'none'
			})
			setTimeout(function (){
				uni.clearStorage()
				uni.reLaunch({
					url: '/pages/login/index',
				})
			},1500)
			}
		},
		fail(res) {
			fail && fail(res)
		}
	})
}
const get = async function(url, data, success, fail, power,contentType) {
	if (url.indexOf('http') == -1) {
		url = baseUrl + url
	}
	var header = {};
	
	if (contentType == 0) {
		header['content-type'] = "application/json;charset=UTF-8";
	}
	if (contentType == 1) {
		header['content-type'] = "application/x-www-form-urlencoded;charset=UTF-8";
	}
	
		var token = uni.getStorageSync('token');
		header['Authorization'] = token;
	
	if (data) {
		// data = "?" + qs.stringify(data);
		data = "?" + `bossId=${bossId}&storeId=${storeId}&` + qs.stringify(data);
		url += data;
	}
	uni.request({
		url,
		method: 'GET',
		data,
		header,
		success(res) {
			if (res.statusCode == 200) {
				success && success(res.data)
			} else {
				fail && fail(res)
			}
			if(res.data.code == 403 || res.data.code == 401){  ////拦截器，403 登陆过期
				uni.showToast({
					title:'登录失效，请重新登录',
					icon:'none'
				})
				setTimeout(function (){
					uni.clearStorage()
					uni.reLaunch({
						url: '/pages/login/index',
					})
				},1500)
			}
		},
		fail(res) {
			fail && fail(res)
		}
	})
}

const put = function(url, data, success, fail, power, contentType) {
	var header = {};

	if (contentType == 0) {
		header['content-type'] = "application/json;charset=UTF-8";
	}
	if (contentType == 1) {
		header['content-type'] = "application/x-www-form-urlencoded;charset=UTF-8";
	}

	
		var token =uni.getStorageSync('token');
		header['Authorization'] = token;
	
	if (data) {
		// data = qs.stringify(data);
	} else {
		data = null;
	}
	uni.request({
		url,
		method: 'PUT',
		data,
		header,
		success(res) {
			if (res.statusCode == 200) {
			
				success && success(res.data)
			} else {
				fail && fail(res)
			}
		},
		fail(res) {
			fail && fail(res)
		}
	})
}

const DELETE = function(url, data, success, fail, power, contentType) {
	var header = {};

	if (contentType == 0) {
		header['content-type'] = "application/json;charset=UTF-8";
	}
	if (contentType == 1) {
		header['content-type'] = "application/x-www-form-urlencoded;charset=UTF-8";
	}
		var token =uni.getStorageSync('token');
		header['Authorization'] = token;
	if (data) {
		// data = qs.stringify(data);
	} else {
		data = null;
	}
	uni.request({
		url,
		method: 'DELETE',
		data,
		header,
		success(res) {
			if (res.statusCode == 200) {
			
				success && success(res.data)
			} else {
				fail && fail(res)
			}
		},
		fail(res) {
			fail && fail(res)
		}
	})
}

module.exports = {
	// 请求方法
	get,
	post,
	put,
	DELETE
}
